package com.group5.onlinegame.handler;

import com.group5.onlinegame.entity.Recharge;
import com.group5.onlinegame.service.RechargeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@Controller
@RequestMapping("/")
public class RechargeHandler {
    @Autowired
    private RechargeService rechargeService;

    @RequestMapping("recharge")
    @RequiresRoles("recharge")
    public ModelAndView getRechargeList(){
        ModelAndView mv=new ModelAndView("rechartype");
        List<Recharge> list=rechargeService.getRechargeList();
        mv.addObject("list",list);
        return mv;
    }


    @RequestMapping("addrecharge")
    @RequiresRoles("recharge")
    public String addRecharge(Recharge recharge){
        if (recharge.getId()==null){
            rechargeService.addRecharge(recharge);
        }else {
            rechargeService.upRecharge(recharge);
        }
        return "redirect:/recharge";
    }

    @RequestMapping("delrecharge")
    @ResponseBody
    @RequiresRoles("recharge")
    public String delRecharge(@RequestParam(value ="listid[]") int[] listid){
        try{
            rechargeService.delRecharge(listid);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }

    @RequestMapping("rechargebyid")
    @ResponseBody
    @RequiresRoles("recharge")
    public Recharge getRechargebyid(@RequestParam(value = "id") int id){

        return rechargeService.getRechargebyid(id);

    }

}
